🎖️GitЯра🎖️
Node / meshtastic / Meshtastic-Android / files / core / network / src / commonTest / kotlin / org / meshtastic / core / network / radio / GattCacheInvalidationGateTest.kt
Displaying Raw • Download
core/network/src/commonTest/kotlin/org/meshtastic/core/network/radio/GattCacheInvalidationGateTest.kt bd2863243bab6eb213401d949839a2bc74dde7e2 (bd286324) Text, 6.22 KB
T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.network.radio
Tff7b72import T7ee787kotlin.test.Test
Tff7b72import T7ee787kotlin.test.assertFailsWith
Tff7b72import T7ee787kotlin.test.assertFalse
Tff7b72import T7ee787kotlin.test.assertTrue
Tff7b72import T7ee787kotlin.time.Duration
Tff7b72import T7ee787kotlin.time.Duration.Companion.minutes
Tff7b72class T56d364GattCacheInvalidationGateTest Tb4b4b4{
Tf0883e@Test
Tff7b72fun Td2a8ff`no invalidation while the failure streak is below the threshold`Tb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3gate Tff7b72= Te6edf3GattCacheInvalidationGateTb4b4b4(Te6edf3failureThreshold Tff7b72= T79c0ff3Tb4b4b4)
Te6edf3assertFalseTb4b4b4(Te6edf3gateTb4b4b4.Te6edf3shouldInvalidateOnAttemptTb4b4b4(T79c0ff0Tb4b4b4)Tb4b4b4, Ta5d6ff"Ta5d6ffa healthy connection must never refresh the cacheTa5d6ff"Tb4b4b4)
Te6edf3assertFalseTb4b4b4(Te6edf3gateTb4b4b4.Te6edf3shouldInvalidateOnAttemptTb4b4b4(Tff7b72-T79c0ff1Tb4b4b4)Tb4b4b4, Ta5d6ff"Ta5d6ffa nonsensical count must never refresh the cacheTa5d6ff"Tb4b4b4)
Te6edf3assertFalseTb4b4b4(Te6edf3gateTb4b4b4.Te6edf3shouldInvalidateOnAttemptTb4b4b4(T79c0ff1Tb4b4b4)Tb4b4b4, Ta5d6ff"Ta5d6ffone failure is an ordinary out-of-range blipTa5d6ff"Tb4b4b4)
Te6edf3assertFalseTb4b4b4(Te6edf3gateTb4b4b4.Te6edf3shouldInvalidateOnAttemptTb4b4b4(T79c0ff2Tb4b4b4)Tb4b4b4, Ta5d6ff"Ta5d6fftwo failures are still below the thresholdTa5d6ff"Tb4b4b4)
Tb4b4b4}
Tf0883e@Test
Tff7b72fun Td2a8ff`invalidation is requested once the threshold is reached`Tb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3gate Tff7b72= Te6edf3GattCacheInvalidationGateTb4b4b4(Te6edf3failureThreshold Tff7b72= T79c0ff3Tb4b4b4)
Te6edf3assertTrueTb4b4b4(Te6edf3gateTb4b4b4.Te6edf3shouldInvalidateOnAttemptTb4b4b4(T79c0ff3Tb4b4b4)Tb4b4b4, Ta5d6ff"Ta5d6ffthe threshold-th consecutive failure must arm the refreshTa5d6ff"Tb4b4b4)
Tb4b4b4}
Tf0883e@Test
Tff7b72fun Td2a8ff`the refresh fires at most once per failure streak`Tb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3gate Tff7b72= Te6edf3GattCacheInvalidationGateTb4b4b4(Te6edf3failureThreshold Tff7b72= T79c0ff3Tb4b4b4)
Te6edf3assertTrueTb4b4b4(Te6edf3gateTb4b4b4.Te6edf3shouldInvalidateOnAttemptTb4b4b4(T79c0ff3Tb4b4b4)Tb4b4b4)
Te6edf3gateTb4b4b4.Te6edf3onCacheInvalidatedTb4b4b4(Tb4b4b4)
Te6edf3assertFalseTb4b4b4(Te6edf3gateTb4b4b4.Te6edf3shouldInvalidateOnAttemptTb4b4b4(T79c0ff4Tb4b4b4)Tb4b4b4, Ta5d6ff"Ta5d6ffa refresh that did not help must not repeat every attemptTa5d6ff"Tb4b4b4)
Te6edf3assertFalseTb4b4b4(Te6edf3gateTb4b4b4.Te6edf3shouldInvalidateOnAttemptTb4b4b4(T79c0ff9Tb4b4b4)Tb4b4b4, Ta5d6ff"Ta5d6ffthe allowance stays consumed for the rest of the streakTa5d6ff"Tb4b4b4)
Tb4b4b4}
T8b949e/**
* Discriminator for the "consume the allowance only on a real refresh" rule: on Android the reflection hop into
* `BluetoothGatt` can miss, in which case nothing was refreshed and nothing may be consumed. Without the guard, one
* silent miss would disable the recovery for the whole streak.
*/
Tf0883e@Test
Tff7b72fun Td2a8ff`an attempted refresh that never happened leaves the allowance intact`Tb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3gate Tff7b72= Te6edf3GattCacheInvalidationGateTb4b4b4(Te6edf3failureThreshold Tff7b72= T79c0ff3Tb4b4b4)
Te6edf3assertTrueTb4b4b4(Te6edf3gateTb4b4b4.Te6edf3shouldInvalidateOnAttemptTb4b4b4(T79c0ff3Tb4b4b4)Tb4b4b4)
T8b949e// No onCacheInvalidated(): the platform reported it could not refresh.
Te6edf3assertTrueTb4b4b4(Te6edf3gateTb4b4b4.Te6edf3shouldInvalidateOnAttemptTb4b4b4(T79c0ff4Tb4b4b4)Tb4b4b4, Ta5d6ff"Ta5d6ffa no-op refresh must not burn the streak allowanceTa5d6ff"Tb4b4b4)
Tb4b4b4}
Tf0883e@Test
Tff7b72fun Td2a8ff`the end of a failure streak re-arms the gate for the next one`Tb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3gate Tff7b72= Te6edf3GattCacheInvalidationGateTb4b4b4(Te6edf3failureThreshold Tff7b72= T79c0ff3Tb4b4b4)
Te6edf3assertTrueTb4b4b4(Te6edf3gateTb4b4b4.Te6edf3shouldInvalidateOnAttemptTb4b4b4(T79c0ff3Tb4b4b4)Tb4b4b4)
Te6edf3gateTb4b4b4.Te6edf3onCacheInvalidatedTb4b4b4(Tb4b4b4)
Te6edf3assertFalseTb4b4b4(Te6edf3gateTb4b4b4.Te6edf3shouldInvalidateOnAttemptTb4b4b4(T79c0ff5Tb4b4b4)Tb4b4b4)
Te6edf3gateTb4b4b4.Te6edf3onFailureStreakEndedTb4b4b4(Tb4b4b4)
Te6edf3assertFalseTb4b4b4(Te6edf3gateTb4b4b4.Te6edf3shouldInvalidateOnAttemptTb4b4b4(T79c0ff2Tb4b4b4)Tb4b4b4, Ta5d6ff"Ta5d6ffthe new streak restarts below the thresholdTa5d6ff"Tb4b4b4)
Te6edf3assertTrueTb4b4b4(Te6edf3gateTb4b4b4.Te6edf3shouldInvalidateOnAttemptTb4b4b4(T79c0ff3Tb4b4b4)Tb4b4b4, Ta5d6ff"Ta5d6ffa fresh streak earns a fresh refreshTa5d6ff"Tb4b4b4)
Tb4b4b4}
Tf0883e@Test
Tff7b72fun Td2a8ff`a threshold of one refreshes on the first failure`Tb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3gate Tff7b72= Te6edf3GattCacheInvalidationGateTb4b4b4(Te6edf3failureThreshold Tff7b72= T79c0ff1Tb4b4b4)
Te6edf3assertTrueTb4b4b4(Te6edf3gateTb4b4b4.Te6edf3shouldInvalidateOnAttemptTb4b4b4(T79c0ff1Tb4b4b4)Tb4b4b4)
Tb4b4b4}
Tf0883e@Test
Tff7b72fun Td2a8ff`a non-positive threshold is rejected`Tb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3assertFailsWithTff7b72<Te6edf3IllegalArgumentExceptionTff7b72> Tb4b4b4{ Te6edf3GattCacheInvalidationGateTb4b4b4(Te6edf3failureThreshold Tff7b72= T79c0ff0Tb4b4b4) Tb4b4b4}
Te6edf3assertFailsWithTff7b72<Te6edf3IllegalArgumentExceptionTff7b72> Tb4b4b4{ Te6edf3GattCacheInvalidationGateTb4b4b4(Te6edf3failureThreshold Tff7b72= Tff7b72-T79c0ff1Tb4b4b4) Tb4b4b4}
Tb4b4b4}
T8b949e/**
* The refresh must sit far above the policy's transient-disconnect threshold. That threshold only decides when to
* tell higher layers a disconnect is more than a blip — it is reached roughly 47 s into an ordinary out-of-range or
* powered-off gap, which is no evidence at all of a stale service table. Refreshing there tears down a link that
* was about to succeed and costs the user a failed attempt plus a fresh round of backoff.
*/
Tf0883e@Test
Tff7b72fun Td2a8ff`the default threshold is far above the reconnect policy transient-disconnect threshold`Tb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3assertTrueTb4b4b4(
Te6edf3GattCacheInvalidationGateTb4b4b4.Te6edf3DEFAULT_FAILURE_THRESHOLD Tff7b72> Te6edf3BleReconnectPolicyTb4b4b4.Te6edf3DEFAULT_FAILURE_THRESHOLDTb4b4b4,
Ta5d6ff"Ta5d6ffa refresh at the transient-disconnect threshold would sabotage ordinary reconnects instead of repairing Ta5d6ff" Tff7b72+
Ta5d6ff"Ta5d6ffa stale cache (cache=Tffd700${Te6edf3GattCacheInvalidationGateTb4b4b4.Te6edf3DEFAULT_FAILURE_THRESHOLDTffd700}Ta5d6ff, Ta5d6ff" Tff7b72+
Ta5d6ff"Ta5d6fftransient=Tffd700${Te6edf3BleReconnectPolicyTb4b4b4.Te6edf3DEFAULT_FAILURE_THRESHOLDTffd700}Ta5d6ff)Ta5d6ff"Tb4b4b4,
Tb4b4b4)
Tb4b4b4}
T8b949e/**
* Pins the intent behind the default rather than the number: the gate may only fire after *minutes* of unbroken
* failure, because the stale cache it repairs follows a prolonged absence (issue #6685 — "go out of range / power
* off"). Derived from the production settle delay and backoff ladder, so retuning either one cannot silently drag
* the refresh back into blip territory.
*/
Tf0883e@Test
Tff7b72fun Td2a8ff`the default threshold is only reachable after minutes of unbroken failure`Tb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3threshold Tff7b72= Te6edf3GattCacheInvalidationGateTb4b4b4.Te6edf3DEFAULT_FAILURE_THRESHOLD
Tff7b72val Te6edf3backoff Tff7b72=
Tb4b4b4(T79c0ff1.Tb4b4b4.Te6edf3thresholdTb4b4b4)Tb4b4b4.Te6edf3foldTb4b4b4(Te6edf3DurationTb4b4b4.Te6edf3ZEROTb4b4b4) Tb4b4b4{ Te6edf3totalTb4b4b4, Te6edf3failures Tff7b72-Tff7b72> Te6edf3total Tff7b72+ Te6edf3computeReconnectBackoffTb4b4b4(Te6edf3failuresTb4b4b4) Tb4b4b4}
T8b949e// One settle delay precedes every attempt: the `threshold` attempts that fail, plus the one that refreshes.
Tff7b72val Te6edf3elapsedBeforeRefresh Tff7b72= Te6edf3BleReconnectPolicyTb4b4b4.Te6edf3DEFAULT_SETTLE_DELAY Tff7b72* Tb4b4b4(Te6edf3threshold Tff7b72+ T79c0ff1Tb4b4b4) Tff7b72+ Te6edf3backoff
Te6edf3assertTrueTb4b4b4(
Te6edf3elapsedBeforeRefresh Tff7b72>Tff7b72= T79c0ff3.Te6edf3minutesTb4b4b4,
Ta5d6ff"Ta5d6ffthe refresh must not be reachable inside an ordinary out-of-range gap (reached at Tffd700$Te6edf3elapsedBeforeRefreshTa5d6ff)Ta5d6ff"Tb4b4b4,
Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
Served by rngit 1.5.0 - Generated in 0.05s